package cn.itrip.biz.controller;

import cn.itrip.beans.dtos.Dto;
import cn.itrip.beans.pojo.ItripHotelRoom;
import cn.itrip.beans.pojo.ItripImage;
import cn.itrip.beans.pojo.ItripLabelDic;
import cn.itrip.beans.vo.ItripImageVO;
import cn.itrip.beans.vo.ItripLabelDicVO;
import cn.itrip.beans.vo.hotelroom.ItripHotelRoomVO;
import cn.itrip.beans.vo.hotelroom.SearchHotelRoomVO;
import cn.itrip.biz.service.itripHotelRoom.ItripHotelRoomService;
import cn.itrip.biz.service.itripImage.ItripImageService;
import cn.itrip.biz.service.itripLabelDic.ItripLabelDicService;
import cn.itrip.common.DateUtil;
import cn.itrip.common.DtoUtil;
import cn.itrip.common.EmptyUtils;
import jdk.nashorn.internal.objects.NativeUint8Array;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

@Controller
@RequestMapping("/api/hotelroom")
public class HotelRoomController {
    @Resource
    private ItripLabelDicService itripLabelDicService;
    @Resource
    private ItripHotelRoomService itripHotelRoomService;
    @Resource
    private ItripImageService itripImageService;

    @RequestMapping(value = "queryhotelroombed", method = RequestMethod.GET, produces = "application/json")
    @ResponseBody
    public Dto<ItripLabelDicVO> queryHotelRoomBed() throws Exception{
        try {
            Map<String, Object> param = new HashMap<String, Object>();
            param.put("parentId",1);
            List<ItripLabelDic> result = itripLabelDicService.getItripLabelDicListByMap(param);
            List<ItripLabelDicVO> output = new ArrayList<ItripLabelDicVO>();
            if(EmptyUtils.isNotEmpty(result)){
                ItripLabelDicVO itripLabelDicVO = null;
                for(ItripLabelDic label:result){
                    itripLabelDicVO = new ItripLabelDicVO();
                    BeanUtils.copyProperties(label,itripLabelDicVO);
                    output.add(itripLabelDicVO);
                }
            }
            return DtoUtil.returnDataSuccess(output);
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("获取酒店房间床型失败","100305 ");
        }
    }

    //查询酒店房型
    @RequestMapping(value = "/queryhotelroombyhotel", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public Dto<List<ItripHotelRoomVO>> queryHotelRoomByHotel(@RequestBody SearchHotelRoomVO searchHotelRoomVO) throws Exception{
        if(searchHotelRoomVO.getHotelId() == null || searchHotelRoomVO.getHotelId() == 0){
            return DtoUtil.returnFail("酒店id不能为空","100303 ");
        }
        if(searchHotelRoomVO.getStartDate() == null || searchHotelRoomVO.getEndDate() == null){
            return DtoUtil.returnFail("酒店入住及退房时间不能为空","100303 ");
        }
        if(searchHotelRoomVO.getStartDate().getTime() > searchHotelRoomVO.getEndDate().getTime()){
            return DtoUtil.returnFail("入住时间不能大于退房时间","100303 ");
        }
        try {
            Map<String, Object> param = new HashMap<>();
            param.put("hotelId", searchHotelRoomVO.getHotelId());
            param.put("roomBedTypeId", searchHotelRoomVO.getRoomBedTypeId());
            param.put("isHavingBreakfast", searchHotelRoomVO.getIsHavingBreakfast());
            param.put("isBook", searchHotelRoomVO.getIsBook());
            param.put("isCancel", searchHotelRoomVO.getIsCancel());
            param.put("isTimelyResponse", searchHotelRoomVO.getIsTimelyResponse());
            param.put("payType", (searchHotelRoomVO.getPayType()==null||searchHotelRoomVO.getPayType()==3)?null:searchHotelRoomVO.getPayType());//1在线付2到店付3不限
           List<Date> timeList = DateUtil.getBetweenDates(searchHotelRoomVO.getStartDate(),
                   searchHotelRoomVO.getEndDate());
            param.put("timeList", timeList);

            List<ItripHotelRoom> result = itripHotelRoomService.getItripHotelRoomListByMap(param);
            List<List<ItripHotelRoomVO>> output = new ArrayList<>();
            if(EmptyUtils.isNotEmpty(result)){
                ItripHotelRoomVO vo = null;
                List<ItripHotelRoomVO> templist = null;
                for(ItripHotelRoom room:result){
                    vo = new ItripHotelRoomVO();
                    BeanUtils.copyProperties(room, vo);
                    templist = new ArrayList<>();
                    templist.add(vo);
                    output.add(templist);
                }
            }
            return DtoUtil.returnDataSuccess(output);
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("系统异常","100304  ");
        }
    }

    //查询酒店房型图片
    @RequestMapping(value = "/getimg/{targetId}", method = RequestMethod.GET, produces = "application/json")
    @ResponseBody
    public Dto<ItripImageVO> getImgForRoom(@PathVariable String targetId) throws Exception{
        if(EmptyUtils.isEmpty(targetId)){
            return DtoUtil.returnFail("酒店房型id不能为空","100302 ");
        }
        try {
            Map<String, Object> param = new HashMap<>();
            param.put("type","1");
            param.put("targetId",Long.valueOf(targetId));
            List<ItripImage> result = itripImageService.getItripImageListByMap(param);
            List<ItripImageVO> output = new ArrayList<>();
            if(EmptyUtils.isNotEmpty(result)){
                ItripImageVO vo = null;
                for(ItripImage image:result){
                    vo = new ItripImageVO();
                    BeanUtils.copyProperties(image, vo);
                    output.add(vo);
                }
            }
            return DtoUtil.returnDataSuccess(output);
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("获取酒店房型图片失败","100301 ");
        }
    }
}
